package com.car.utils;

import com.car.domain.Car;
import com.car.domain.Employee;
import com.car.domain.PurchaseRecord;
import com.car.domain.User;
import com.car.service.EmployeeService;
import com.car.service.PurchaseRecordService;
import com.car.service.StoreService;
import com.car.service.UserService;

import java.util.List;
import java.util.Scanner;

public class MenuUtil {
    private static final UserService userService = new UserService();
    private static final StoreService storeService = new StoreService();
    private static final PurchaseRecordService purchaseRecordService = new PurchaseRecordService();
    private static final EmployeeService employeeService = new EmployeeService();

    public static void mainMenu() throws Exception {
        Scanner scanner = new Scanner(System.in);
        int n;
        while (true) {
            System.out.println("===========汽车销售系统===========");
            System.out.println("1.用户登录");
            System.out.println("2.用户注册");
            System.out.println("3.员工登录");
            System.out.println("0.退出");
            System.out.println("================================");
            System.out.println("请输入指令：");
            n = scanner.nextInt();
            if (n == 0) {
                break;
            } else if (n == 1) {
                // 用户登录
                // 输入账号 密码
                // select * from t_user where phone = ? and password = ?
                User user = userService.login(scanner);
                if (user == null) {
                    System.out.println("登录失败。");
                } else {
                    System.out.println("登录成功");
                    // 登录成功后的操作
                    userMenu(user, scanner);
                }
            } else if (n == 2) {
                // 用户注册
                userService.register();
            } else if (n == 3) {
                //  员工登录
                Employee employee = employeeService.login(scanner);
                if (employee != null) {
                    employeeMenu(employee, scanner);
                }
            } else {
                System.out.println("输入错误，请重新输入。");
            }
        }
    }

    private static void employeeMenu(Employee employee, Scanner scanner) throws Exception {
        int n;
        while (true) {
            System.out.println("=============员工功能============");
            System.out.println("1.商店汽车");
            System.out.println("2.上架汽车");
            System.out.println("3.购买记录");
            System.out.println("0.退出");
            System.out.println("================================");
            System.out.println("请输入指令：");
            n = scanner.nextInt();
            if (n == 0) {
                break;
            } else if (n == 1) {
                // 商店汽车
                printCars(storeService.display());
            } else if (n == 2) {
                employeeService.addCar(employee, scanner);
            } else if (n == 3) {
                List<PurchaseRecord> list = purchaseRecordService.list();
                for (PurchaseRecord pr : list) {
                    System.out.println(pr);
                }
            } else {
                System.out.println("输入错误");
            }
        }
    }

    private static void userMenu(User user, Scanner scanner) throws Exception {
        int n;
        while (true) {
            System.out.println("=============用户功能============");
            System.out.println("1.商店汽车");
            System.out.println("2.购买汽车");
            System.out.println("3.购买记录");
            System.out.println("4.我的汽车");
            System.out.println("5.修改密码");
            System.out.println("6.修改用户名");
            System.out.println("0.退出");
            System.out.println("================================");
            System.out.println("请输入指令：");
            n = scanner.nextInt();
            if (n == 0) {
                break;
            } else if (n == 1) {
                // 商店汽车
                printCars(storeService.display());
            } else if (n == 2) {
                // 购买汽车
                userService.buyCar(user, scanner);
            } else if (n == 3) {
                //购买记录
                List<PurchaseRecord> purchaseRecordList = purchaseRecordService.findByUserId(user.getId());
                System.out.println(purchaseRecordList);
            } else if (n == 4) {
                // 我的汽车
                printCars(userService.ownCars(user));
            } else if (n == 5) {
                // 修改密码
                userService.updatePassword(user, scanner);
            } else if (n == 6) {
                // 修改用户名
                userService.updateUserName(user, scanner);
            } else {
                System.out.println("输入错误，请重新输入");
            }
        }
    }

    private static void printCars(List<Car> list) {
        System.out.println("id\tname\tbrand\tprice\ttype\tcolor\tcarNum\tstatus\temployeeId");
        for (Car car : list) {
            System.out.println(car.getId() + "\t" + car.getName() + "\t" + car.getBrand() + "\t" + car.getPrice() + "\t" +
                    car.getType() + "\t" + car.getCarNum() + "\t" + car.getStatus() + "\t" + car.getEmployeeId());
        }
    }
}
